/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.logging;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author Ondra
 */
public class Logging {

    //hlavni logger celeho programu
    private static Logger mainLogger;
    private static FileHandler fileHandler;

    //vrati logger, pokud neexistuje, vytvori ho
    public static Logger getMainLogger() {
        if (mainLogger == null) {
//            System.out.println("mainLogger je null");
            createMainLogger();
        }
        return mainLogger;
    }

    //vytvari logger, ktery bude zapisovat do konzole (to dela automaticky) a do souboru ve slozce, kde mate projekt
    //soubor se maze po kazdem restartu programu (pise znova), pripada mi to tak lepsi
    private static void createMainLogger() {
        mainLogger = Logger.getLogger("Main_Logger");
        try {
            fileHandler = new FileHandler("dpds_log.txt", false);
            fileHandler.setFormatter(new SimpleFormatter());
            mainLogger.addHandler(fileHandler);
        } catch (Exception ex) {   //fileHandler rad hazi IOExceptiony
            System.out.println("file logging creation failed because of: " + ex.getLocalizedMessage());
        }
        
        mainLogger.log(Level.INFO, "Started logging at {0}", System.currentTimeMillis());
        flushAllHandlers();
    }

    //flushne zpravy -> volejte na konci KAZDEHO logovani
    public static void flushAllHandlers() {
        fileHandler.flush();
    }
}
